package com.example.demo.suanfa_sort;

/**
 * 二分搜索
 */
public class BSExistDemo {

    public static boolean exist(int[] sortedArr, int num) {
        if (sortedArr == null || sortedArr.length == 0) {
            return false;
        }
        int L = 0;
        int R = sortedArr.length - 1;
        int mid = 0;
        // L..R
        while (L < R) {
            mid = L + ((R - L) >> 1);
            if (sortedArr[mid] > num) {
                R = mid -1;
            } else if (sortedArr[mid] < num) {
                L = mid + 1;
            } else {
                return true;
            }
        }
        return sortedArr[L]==num;
    }


}
